package the.workshop.tms.goods.controller;

import org.springframework.web.bind.annotation.*;
import org.wisteria.framework.web.APIResult;
import org.wisteria.framework.web.QueryResult;
import the.broccoli.auth.UserContext;
import the.workshop.tms.goods.dto.GoodsCondition;
import the.workshop.tms.goods.dto.GoodsDTO;
import the.workshop.tms.goods.entity.Goods;
import the.workshop.tms.goods.service.IGoodsService;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("goods")
public class GoodsController {
    @Resource
    private IGoodsService goodsService;

    @PostMapping("/add")
    public APIResult add(@ModelAttribute GoodsDTO dto) {
        goodsService.save(dto.toGoods());
        return APIResult.success();
    }

    @PostMapping("/modify")
    public APIResult modify(@ModelAttribute GoodsDTO dto) {
        goodsService.updateById(dto.toGoods());
        return APIResult.success();
    }

    @PostMapping("/remove")
    public APIResult remove(@RequestParam(value = "ids", required = true) List<Long> ids) {
        goodsService.removeByIds(ids, UserContext.userId(), UserContext.userName());
        return APIResult.success();
    }

    @GetMapping("/{id}")
    public APIResult<Goods> get(@PathVariable(value = "id") Long id) {
        return APIResult.success(goodsService.getById(id));
    }

    @GetMapping("/page")
    public APIResult<QueryResult<Goods>> page(@ModelAttribute GoodsCondition condition) {
        return APIResult.success(goodsService.pageList(condition, condition.getPageNum(), condition.getPageSize()));
    }
}
